Java Program Execution Flow
What Happens When You Run Java Codeβ
Most people write Java code but donβt really know what happens when they run it.
This file explains:
- What happens from
.javafile to output - Role of compiler, JVM, and runtime
- Why Java errors happen at different stages
This understanding removes confusion later in:
- debugging
- JVM topics
- performance discussions
- interview questions
High-Level Execution Flowβ
When you run a Java program, this is what actually happens:
.java β compiler β .class (bytecode) β JVM β output
Each step has a clear responsibility.
Step 1: Writing Source Code (.java)β
You write Java code in a .java file.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello");
}
}
At this stage:
- Code is human-readable
- JVM cannot run it directly
Step 2: Compilation (javac)β
The Java compiler (javac) converts source code into bytecode.
Command:
javac HelloWorld.java
Output:
HelloWorld.class
Important:
- Compilation checks syntax errors
- No JVM execution happens yet
What Is Bytecode (Important Concept)β
Bytecode:
- is NOT machine code
- is NOT human-readable
- is platform-independent
Bytecode is designed to be understood by the JVM, not the OS.
This is the key to Java portability.
Step 3: Class Loadingβ
When you run:
java HelloWorld
The JVM:
- Finds the
.classfile - Loads it into memory
- Verifies bytecode safety
This is handled by the Class Loader subsystem.
You donβt control this manually.
Step 4: Bytecode Verificationβ
Before execution, JVM verifies:
- no illegal memory access
- no stack corruption
- no security violations
If verification fails: β program does not run
This makes Java safer than many languages.
Step 5: Execution (Interpreter + JIT)β
The JVM executes bytecode using:
- Interpreter (initial execution)
- JIT compiler (optimizes frequently used code)
Important:
Java is both interpreted and compiled
This is why Java performance improves over time.
Role of JIT Compiler (Conceptual)β
JIT:
- converts hot bytecode into native machine code
- improves performance
- runs automatically
You donβt write JIT code. The JVM handles it.
Runtime Errors vs Compile-Time Errorsβ
Compile-Time Errorsβ
Detected by javac:
- syntax errors
- missing semicolons
- type mismatches
Runtime Errorsβ
Detected by JVM:
- NullPointerException
- ArrayIndexOutOfBoundsException
- OutOfMemoryError
Different stages, different responsibilities.
Where main() Fits Inβ
main() is:
- JVM entry point
- NOT a special Java keyword
- required to start execution
Without main():
β program wonβt start
Why This Knowledge Mattersβ
Understanding execution flow helps you:
- debug errors faster
- understand JVM topics later
- answer interview questions clearly
- reason about performance
Without this, Java feels βmagicalβ.
Common Mistakesβ
- Thinking JVM compiles Java code
- Confusing compiler with JVM
- Believing Java is purely interpreted
- Ignoring bytecode completely
Interview Notesβ
- Steps of Java program execution
- What is bytecode?
- Role of JVM vs compiler
- Why Java is platform-independent
- Compile-time vs runtime errors
Summaryβ
Java execution is a pipeline, not a single step.
Understanding this pipeline is foundational for mastering Java.